var bt = baidu.template;

function Products (option) {
    this.productName = $('#product-list-nav').find('.cur').data('name');
    this.loadUrl = $('#load-more').attr('data-url');
    this.loadPage = 0;
    this.loadTimes = 0;
    this._init();
}
Products.prototype = {
    _init: function () {
        var _self = this;
        var productsContent = '';
        $('#load-more').on('click', function(e) {
            e.preventDefault();
            _self.loadProducts();
        });
    },
    getProducts: function (page) {
        var _self = this;
        if ( _self.loadPage === null ) {
            $('#load-more').hide();
            return false;
        }
        page = page || 0;
        $.get(_self.loadUrl + page, function(data) {
            console.log(data);
            _self.productsContent = bt('product_template', {list: data.products,type: _self.productName});
            _self.handleDOM();
            _self.loadPage = data.next_page;
            if ( _self.loadTimes === 0 ) {
                console.log(0);
                _self.loadProducts();
            }
            _self.loadTimes++;
        });
    },
    loadProducts: function () {
        var _self = this;
        $(_self.productsContent).appendTo('#product-list');
        _self.getProducts(_self.loadPage);
    },
    setCountDown: function ( time, id ) {
        var _self = this;
        var _obj = {
            product: id,
            sec: document.getElementById("sec-"+id),
            mini: document.getElementById("mini-"+id),
            hour: document.getElementById("hour-"+id),
            day: document.getElementById("day-"+id)
        };
        _self.fnTimeCountDown(time, _obj);
    },
    handleDOM: function() {
        var _self = this;
        var _li = $('#product-list').find('li');
        $.each(_li, function(i, val) {
            if ( $(this).data('time') ) {
                var _id = $(this).data('id'),
                    _time = $(this).data('time') * 1000;
                _self.setCountDown( _time, _id );
                $(this).find('.countdown p').removeClass('hide');
            }else{
                $(this).find('.countdown p').hide();
            }
        });
    },
    fnTimeCountDown: function(d, o) {
        var f = {
            zero: function(n) {
                var n = parseInt(n, 10);
                if (n > 0) {
                    if (n <= 9) {
                        n = "0" + n;
                    }
                    return String(n);
                } else {
                    return "00";
                }
            },
            dv: function() {
                d = d || Date.UTC(2050, 0, 1);
                var future = new Date(d),
                    now = new Date();
                //现在将来秒差值
                var dur = Math.round((future.getTime() - now.getTime()) / 1000) + future.getTimezoneOffset() * 60,
                    pms = {
                        sec: "00",
                        mini: "00",
                        hour: "00",
                        day: "00",
                        month: "00",
                        year: "0"
                    };
                if (dur > 0) {
                    pms.sec = f.zero(dur % 60);
                    pms.mini = Math.floor((dur / 60)) > 0 ? f.zero(Math.floor((dur / 60)) % 60) : "00";
                    pms.hour = Math.floor((dur / 3600)) > 0 ? f.zero(Math.floor((dur / 3600)) % 24) : "00";
                    pms.day = Math.floor((dur / 86400)) > 0 ? f.zero(Math.floor((dur / 86400))) : "00";
                }else if(dur < 0 && $(o.sec).parents('.countdown').hasClass('countdown-seckill') ){
                    $(o.sec).parents('p').html('抢购进行中');
                }
                return pms;
            },
            ui: function() {
                if (o.sec) {
                    o.sec.innerHTML = f.dv().sec;
                }
                if (o.mini) {
                    o.mini.innerHTML = f.dv().mini;
                }
                if (o.hour) {
                    o.hour.innerHTML = f.dv().hour;
                }
                if (o.day) {
                    o.day.innerHTML = f.dv().day;
                }
                setTimeout(f.ui, 1000);
            }
        };
        f.ui();
    }
};

var ProductList = new Products();
ProductList.getProducts();